/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddLayerForm.java
 *
 * Created on May 11, 2011, 1:53:14 PM
 */
package tasks.ui.architecture;

import common.DomainException;
import domain.DomainFacade;
import domain.DomainFacadeImpl;
import domain.UnitType;
import domain.UnitTypeDTO;

/**
 *
 * @author David
 */
public class AddUnitForm extends javax.swing.JPanel {

    /** Creates new form AddLayerForm */
    public AddUnitForm() {

        initComponents();
        init();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addUnitLabel = new javax.swing.JLabel();
        addUnitField = new javax.swing.JTextField();
        addUnitButton = new javax.swing.JButton();
        addUnitErrorMsg = new javax.swing.JLabel();
        TypeBox = new javax.swing.JComboBox();

        addUnitLabel.setText("Name:");

        addUnitField.setText("Unit Name");

        addUnitButton.setText("Add unit");
        addUnitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addUnitButtonActionPerformed(evt);
            }
        });

        addUnitErrorMsg.setForeground(java.awt.Color.red);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(addUnitLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addUnitField, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TypeBox, 0, 91, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addUnitButton)
                .addGap(20, 20, 20))
            .addGroup(layout.createSequentialGroup()
                .addComponent(addUnitErrorMsg)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(addUnitErrorMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addUnitLabel)
                    .addComponent(addUnitField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addUnitButton)
                    .addComponent(TypeBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addUnitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addUnitButtonActionPerformed

        if (this.controller.getSelectedLayer() != null) {
            try {
                this.controller.addUnitToLayer(this.addUnitField.getText(), TypeBox.getSelectedItem().toString());
                addUnitErrorMsg.setText("");
            } catch (DomainException de) {
                addUnitErrorMsg.setText(de.getMessage());
            }

        } else {
            addUnitErrorMsg.setText("The unit cannot be added, because there is no layer selected.");
        }
    }//GEN-LAST:event_addUnitButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox TypeBox;
    private javax.swing.JButton addUnitButton;
    private javax.swing.JLabel addUnitErrorMsg;
    private javax.swing.JTextField addUnitField;
    private javax.swing.JLabel addUnitLabel;
    // End of variables declaration//GEN-END:variables
    private ArchitectureController controller;
    private DomainFacade facade;

    public void setController(ArchitectureController controller) {
        this.controller = controller;
    }

    private void init() {
        this.facade = DomainFacadeImpl.getInstance();
        for (UnitTypeDTO type : this.facade.getUnitTypes()) {
            this.TypeBox.addItem(type.getName());
        }
    }
}